package org.database.filter.service.impl;

import java.util.List;

import org.database.filter.model.QuocGia;
import org.database.filter.repository.QuocGiaRepository;
import org.database.filter.service.QuocGiaService;
import org.database.filter.service.TextFilterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class QuocGiaServiceImpl implements QuocGiaService{
	@Autowired
	private QuocGiaRepository quocGiaRepository;
	
	@Autowired
	private TextFilterService textFilterService;
	@Override
	public Integer saveOrUpdate(QuocGia qg) {
		qg.setTEN_QUOC_GIA(textFilterService.doFilterQuocGia(qg.getTEN_QUOC_GIA()));
		List<QuocGia> list = quocGiaRepository.getQuocGia(qg.getTEN_QUOC_GIA());
		if (list == null || list.isEmpty()) {
			QuocGia n = quocGiaRepository.save(qg);
			return n.getMA_QG();
		}
		return list.get(0).getMA_QG();
	}
	
}
